package jp.co.sharp.android.parents.kidsguard.activity.setting.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.activity.setting.ContactOptActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.NotificationContactActivity;
import jp.co.sharp.android.parents.kidsguard.common.DelDeviceConfirmDlg;
import jp.co.sharp.android.parents.kidsguard.dto.ContactListsDto;
import jp.co.sharp.android.parents.kidsguard.dto.DeviceListDto;

public class NotificationContactItemAdaptor extends ArrayAdapter<ContactListsDto> {

    private  int resourceId;
    private OnClickItemListenter onClickItemListenter;
    private Context mContext;

    public NotificationContactItemAdaptor(@NonNull Context context, int resource) {
        super(context, resource);
    }

    public NotificationContactItemAdaptor(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public NotificationContactItemAdaptor(@NonNull Context context, int resource, @NonNull ContactListsDto[] objects) {
        super(context, resource, objects);
    }

    public NotificationContactItemAdaptor(@NonNull Context context, int resource, int textViewResourceId, @NonNull ContactListsDto[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public NotificationContactItemAdaptor(@NonNull Context context, int resource, @NonNull List<ContactListsDto> objects,OnClickItemListenter onClickItemListenter) {
        super(context, resource, objects);
        this.resourceId = resource;
        this.onClickItemListenter = onClickItemListenter;
        this.mContext = context;
    }

    public NotificationContactItemAdaptor(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<ContactListsDto> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        ContactListsDto contactListsDto = getItem(position);

        View view;
        ViewHolder viewHolder;
        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new NotificationContactItemAdaptor.ViewHolder();

            viewHolder.itemName = view.findViewById(R.id.item_name);
            viewHolder.contact =view.findViewById(R.id.contact);
            viewHolder.show    =view.findViewById(R.id.show);
            viewHolder.contactChoose = view.findViewById(R.id.contactChoose);
            viewHolder.del = view.findViewById(R.id.del);
            view.setTag(viewHolder);

        }else {
            view = converView;
            viewHolder = (ViewHolder)view.getTag();
        }
//        if(converView!=null && parent!=null){
            viewHolder.del.setOnClickListener(new Delete(view));
//        }

        viewHolder.itemName.setText(contactListsDto.getName());
        viewHolder.contact.setText("連絡先"+(position+1));

        viewHolder.contactChoose.setOnClickListener(new ChooseContactClick());


        if(contactListsDto.getName() != null && !contactListsDto.getName().equals("設定なし")){
            viewHolder.show.setVisibility(View.VISIBLE);
        }else {
            viewHolder.show.setVisibility(View.INVISIBLE);
        }

        viewHolder.contactChoose.setTag(position);
        viewHolder.del.setTag(position);
        return view;
    }

    class ViewHolder{
        LinearLayout show;
        LinearLayout contactChoose;
        TextView itemName;
        TextView contact;
        ImageView del;
    }

    public interface OnClickItemListenter {
        void chooseItemClick(int position);
    }

    class ChooseContactClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            int ContactOptRequestCode = 1;
            //to view
            Intent intent = new Intent(mContext, ContactOptActivity.class);
            intent.putExtra("position",(Integer)v.getTag());
            NotificationContactActivity.mPageJumpFlag = true;
            ((Activity) mContext).startActivityForResult(intent, ContactOptRequestCode);
        }
    }

    class Delete implements View.OnClickListener{
        View view ;
        Delete(View v){
            view=v;
        }
        @Override
        public void onClick(View v) {
            String message = "通知する連絡先を削除します。よろしいですか？";
            DelDeviceConfirmDlg mDelDeviceConfirmDlg = new DelDeviceConfirmDlg(mContext, R.style.commonDlg, message);
            mDelDeviceConfirmDlg.show();
            mDelDeviceConfirmDlg.setOnOperateSetListener(new DelDeviceConfirmDlg.OnOperateSetListener() {
                @Override
                public void onOperateSet() {
                    ContactListsDto contactListsDto =  getItem((Integer) v.getTag());
                    contactListsDto.setName("設定なし");
                    onClickItemListenter.chooseItemClick((Integer) v.getTag());
                    LinearLayout linearLayout = view.findViewById(R.id.show);
                    linearLayout.setVisibility(View.INVISIBLE);
                    TextView textView = view.findViewById(R.id.item_name);
                    textView.setText("設定なし");
                    mDelDeviceConfirmDlg.dismiss();
                }
            });

        }
    }

}































